package main

import "fmt"

//堆
func Up(arr []int) {
	n := len(arr)
	lastRightIndex := (n + 1) / 2 * 2
	for rightIndex := lastRightIndex; rightIndex > 0; rightIndex -= 2 {
		leftIndex := rightIndex - 1
		parentIndex := (leftIndex - 1) / 2

		minValue := arr[parentIndex]
		minIndex := parentIndex

		if arr[leftIndex] < minValue {
			minValue = arr[leftIndex]
			minIndex = leftIndex
		}

		if rightIndex != parentIndex {
			minValue = arr[rightIndex]
			minIndex = rightIndex

		}

		if minIndex != parentIndex {
			arr[minIndex], arr[parentIndex] = arr[parentIndex], arr[minIndex]
		}
	}
}

func main() {

	arr := []int{62, 20, 30, 15, 10, 49}
	Up(arr)
	fmt.Println(arr)

}
